﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BML.Infrastructure;
using BML.Infrastructure.Platform;
using Service.Infrastructure.Interface;
using Service.Infrastructure.Model;
using Service.Infrastructure.Result;

namespace Service.Infrastructure.Implement
{
    public class ExceptionLogConfigService : BaseService, ILog
    {
        protected SystemicLoggerOp SystemicLoggerOp = new SystemicLoggerOp();

        #region Exp LEVEL
        /// <summary>
        /// const的值是在编译期间确定的，因此只能在声明时通过常量表达式指定其值。而static readonly是在运行时计算出其值的，所以还可以通过静态构造函数来赋值
        /// </summary>
        public const Int32 ExpLevelRuntime = 1000;

        #endregion



        public ServiceResult QueryLogs()
        {
            return Result(ResultType.OperateQueryIsSuccess, SystemicLoggerOp.QueryExpLogs().Select(m => new ExceptionLogModel
            {
                Id = m.Id,
                Content = m.ExMag,
                Date = m.CreateTime.ToLongDateString(),
                Status = m.Status,
                Time = m.CreateTime.ToLongTimeString(),
                Title = m.OtherInfo
            }).ToList());
        }

        public ServiceResult ProcessingLog(Guid id)
        {
            SystemicLoggerOp.ProcessingExceptionLog(id, OpertorStatusCode.Delete, "");
            return Result(ResultType.OperateDeleteIsSuccess);
        }

        public ServiceResult Logging(Object info, string otherInfo)
        {
            SystemicLoggerOp.LoggingException(FormatExceptionInfo((Exception)info), ExpLevelRuntime, otherInfo);
            return Result(ResultType.OperateAddIsSuccess);
        }

        /// <summary>
        ///     格式化异常信息
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        private String FormatExceptionInfo(Exception e)
        {
            return string.Format("<< [TYPE]:[{0}] << [INFO]:[{1}] << [STACKTRACE]:[{2}] << ", e.GetType(), e.Message, e.StackTrace);
        }
    }
}
